package com.ting.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ting.Exception.UnauthorizedException;
import com.ting.entity.Jwt;
import com.ting.mapper.JwtMapper;
import com.ting.service.JwtService;
import com.ting.untils.JwtUtil;
import com.ting.untils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaobai
 * @since 2021-12-13
 */
@RestController
public class JwtController {

    @Resource
    private JwtMapper jwtMapper;

    @PostMapping("/login")
    public Response login(@RequestParam("name") String name,
                          @RequestParam("password") String password,
                          HttpServletResponse httpServletResponse) throws Exception{
        QueryWrapper<Jwt> wrapper = new QueryWrapper<>();
        wrapper

                .eq("name",name)
                .eq("password",password);
        Jwt jwt = jwtMapper.selectOne( wrapper );

        if (jwt.getPassword().equals(password)){
            return new Response(200,"登陆成功","token为:"+ JwtUtil.createToken(jwt,JwtUtil.SECRET,30) );
        }else {
            throw new UnauthorizedException();
        }
    }
}
